package dji.ux.ext.setting.rc;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import dji.ux.beta.R;
import dji.ux.beta.base.ConstraintLayoutWidget;
import dji.ux.ext.base.view.SelectedOptionPanel;

public class CustomerC1C2Panel extends ConstraintLayoutWidget {

    private Button btnC1;
    private Button btnC2;

    public CustomerC1C2Panel(Context context) {
        super(context);
    }

    public CustomerC1C2Panel(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomerC1C2Panel(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        View view = inflate(context, R.layout.ext_panel_customer_btn, this);
        setClickable(true);
        setFocusable(true);

        btnC1 = view.findViewById(R.id.btn_c1);
        btnC2 = view.findViewById(R.id.btn_c2);

        btnC1.setOnClickListener(view1 -> showPopWindow(btnC1));
        btnC2.setOnClickListener(view1 -> showPopWindow(btnC2));

        setBackgroundColor(Color.BLACK);
        setPadding(25, 25, 25, 25);
    }

    @Override
    protected void reactToModelChanges() {

    }

    @NonNull
    @Override
    public String getIdealDimensionRatioString() {
        return null;
    }

    private void showPopWindow(View view) {
        PopupWindow popupWindow = new PopupWindow();

        SelectedOptionPanel panel = new SelectedOptionPanel(getContext());
        panel.setCallback((index, result) -> {
            popupWindow.dismiss();
        });

        panel.setCurrentText(((Button) view).getText().toString().trim());

        List<String> data = new ArrayList<>();
        data.add("回中/朝下");
        data.add("FPV模式/跟随模式");
        data.add("AE锁定/解锁");
        data.add("打开/关闭机头指示灯");
        data.add("未定义");
        panel.setData(data);

        popupWindow.setContentView(panel);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setWidth(view.getMeasuredWidth());
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.showAsDropDown(view, 0, -view.getMeasuredHeight());
    }
}
